package org.csu.mypetstore.controller;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/order")
public class OrderController {

    private Object Model;
    @Autowired
    private OrderService orderService;
    @Autowired
    CatalogService cartService;

    //----订单确定----//
    @RequestMapping("/confirmOrder")
    public String confirmOrder(HttpServletRequest request,Model model) throws UnsupportedEncodingException {
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute("account");
        model.addAttribute("account",account);
        session.setAttribute("account",account);
        Order order = (Order)session.getAttribute("order");
        request.setCharacterEncoding("UTF-8");
        String cardType = request.getParameter("cardType");
        String creditCard = request.getParameter("creditCard");
        String expiryDate = request.getParameter("expiryDate");
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String address1 = request.getParameter("address1");
        String address2 = request.getParameter("address2");
        String city = request.getParameter("city");
        String state = request.getParameter("state");
        String zip = request.getParameter("zip");
        String country = request.getParameter("country");
        String shipToFirstName=request.getParameter("shipToFirstName");
        String shipToLastName =request.getParameter("shipToLastName");
        String shipAddress1 =request.getParameter("shipAddress1");
        String shipAddress2 =request.getParameter("shipAddress2");
        String shipCity =request.getParameter("shipCity");
        String shipState =request.getParameter("shipState");
        String shipZip =request.getParameter("shipZip");
        String shipCountry=request.getParameter("shipCountry");

        if(request.getParameter("shippingAddressRequired") != null){
            if(creditCard==""||expiryDate==""||firstName==""||lastName==""||address1==""||address2==""||city==""||state==""||zip==""||country==""
                    ||shipAddress1==""||shipCity==""||shipAddress2==""||shipState==""||shipZip==""||shipCountry==""||shipToFirstName==""||shipToLastName==""){
                session.setAttribute("messages","Please input credit card or expiry date or other information!");
                return "order/newOrderForm";
            }
        }else{
            if(creditCard==""||expiryDate==""||firstName==""||lastName==""||address1==""||address2==""||city==""||state==""||zip==""||country==""){
                session.setAttribute("messages","Please input credit card or expiry date or other information!");
                return "order/newOrderForm";
            }
        }

        if(firstName!=null) {
            order.setCardType(cardType);
            order.setCreditCard(creditCard);
            order.setExpiryDate(expiryDate);
            order.setBillToFirstName(firstName);
            order.setBillToLastName(lastName);
            order.setBillAddress1(address1);
            order.setBillAddress2(address2);
            order.setBillCity(city);
            order.setBillState(state);
            order.setBillZip(zip);
            order.setBillCountry(country);
            order.setShipToFirstName(request.getParameter("firstName"));
            order.setShipToLastName(request.getParameter("lastName"));
            order.setShipAddress1(request.getParameter("address1"));
            order.setShipAddress2(request.getParameter("address2"));
            order.setShipCity(request.getParameter("city"));
            order.setShipState(request.getParameter("state"));
            order.setShipZip(request.getParameter("zip"));
            order.setShipCountry(request.getParameter("country"));
        }
        session.setAttribute("order",order);

        if(request.getParameter("shippingAddressRequired") != null){
            order.setShipToFirstName(null);
            order.setShipToLastName(null);
            order.setShipAddress1(null);
            order.setShipAddress2(null);
            order.setShipCity(null);
            order.setShipState(null);
            order.setShipZip(null);
            order.setShipCountry(null);
            order.setShipToFirstName(request.getParameter("shipToFirstName"));
            order.setShipToLastName(request.getParameter("shipToLastName"));
            order.setShipAddress1(request.getParameter("shipAddress1"));
            order.setShipAddress2(request.getParameter("shipAddress2"));
            order.setShipCity(request.getParameter("shipCity"));
            order.setShipState(request.getParameter("shipState"));
            order.setShipZip(request.getParameter("shipZip"));
            order.setShipCountry(request.getParameter("shipCountry"));
            session.setAttribute("order",order);
            return "order/confirmOrder";
        }else {
            session.setAttribute("order",order);
            return "order/confirmOrder";
        }
    }

    //----订单显示----//
    @RequestMapping("/listOrders")
    public String listOrder(HttpServletRequest request,Model model){
        List<Order> orderList= new ArrayList<>();
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute("account") ;
        model.addAttribute("account",account);

        orderList=orderService.getOrdersByUsername(account.getUsername());
        session.setAttribute("orderList",orderList);

        if(orderList.size()==0){
            session.setAttribute("message","Sorry, you have no orders now.");
        }else {
            account = (Account)session.getAttribute("account");
        }
        session.setAttribute("account",account);
        return "order/listOrders";
    }

    //----订单界面----//
    @RequestMapping("/newOrderForm")
    public String newOrder(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        Cart cart;
        Account account = (Account) session.getAttribute("account") ;
        model.addAttribute("account",account);

        cart = (Cart)session.getAttribute("cart");
        Order order = new Order();
        order.initOrder(account, cart);
        session.setAttribute("order", order);
        account = (Account)session.getAttribute("account");

        session.setAttribute("account",account);
        return "order/newOrderForm";
    }

    //----查看订单----//
    @RequestMapping("/viewOrder")
    public String viewOrder(HttpServletRequest req,Model model){
        HttpSession session = req.getSession();
        Order order = (Order) session.getAttribute("order");

        Account account = (Account) session.getAttribute("account") ;
        model.addAttribute("account",account);
        session.setAttribute("account",account);
        session.setAttribute("lineitems",order.getLineItems());
        orderService.insertOrder(order);

        //----重置购物车----//
        Cart cart=new Cart();
        session.removeAttribute("cart");

        String username = (account == null) ? "" : account.getUsername();
        cartService.removeByUsername(account);
        return "order/viewOrder";
    }
}